package erik.util;

public class Arrays {

	private Arrays() {}
	/**
	 * Search sorted array arr using default comparator
	 */
	public static <T extends Comparable<? super T>> int binarySearch(T[] arr, T x) {
		return binarySearch(arr, x, new Collections.DefaultComparator<T>());
	}
	private static <T> int binarySearch(T[] arr, T x, Comparator<? super T> cmp) {
		int low = 0, mid = 0;
		int high = arr.length;
		
		while (low < high) {
			mid = (low + high) / 2;
			if (cmp.compare(x, arr[mid]) < 0)
				high = mid;
			else
				low = high - 1;
		}
		if (low == arr.length || cmp.compare(x, arr[low]) != 0)
			return -(low + 1);
		return low;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
